package client;

import java.io.*;
import java.net.*;

public class Client
{
    public static void main(String[] args) throws IOException
    {
       	String host = "169.234.14.7";
//    	String host = "169.234.15.29";

    	int port = 4444;
    	String username = "";
    	String destination = "";
    	
        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket(host, port);
            kkSocket.setKeepAlive(true);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + host);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + host);
            System.exit(1);
        }
        
        System.out.println("Connected to host " + host + " on port " + port);
        
        // print out the server address
		InetAddress i = InetAddress.getLocalHost();
		
		System.out.println("Your computer name: " + i.getHostName());
		System.out.println("Your computer IP: " + i.getHostAddress());
		System.out.println();

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer = "";
        String fromUser = "";
        
        // set the user's name
        System.out.print("What is your name? ");
        username = stdIn.readLine();
        out.println("SETNAME:" + username);
        
        // set the user's destination
        System.out.print("Who are you chatting with? ");
        destination = stdIn.readLine();
        
        System.out.println("Type \"Bye\" to quit");
        
        while(!fromUser.equals("Bye"))
        {
        	System.out.print(username + ": ");
            fromUser = stdIn.readLine();
            
            String message = "<message>";
            
            if (fromUser.equals("addb"))
            {
            	message += "<type>AddBuddy</type>";
                message += "<users>";
                message += "<from><username>" + username + "</username></from>";
                message += "<to><username>" + destination + "</username></to>";
                message += "</users>";
                message += "</message>";
            }
            else {
            // send a Chat message
//            String message = "<message>";
            message += "<type>Chat</type>";
            message += "<users>";
            message += "<from><username>" + username + "</username></from>";
            message += "<to><username>" + destination + "</username></to>";
            message += "</users>";
            message += "<timestamp>right now</timestamp>";
            message += "<text>" + fromUser + "</text>";
            message += "</message>";
            }
            
            // pipe the message out to the socket
            out.println(message);
            
            fromServer = in.readLine(); // read a line from the server
            
            if(fromServer != null)
            {
            	System.out.println(fromServer);
            }
        }

        System.out.println("Messaging socket terminated");
        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}

